package com.example.gslih.componentproject.main.index;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.CpuUsageInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gslih.componentproject.R;
import com.example.gslih.componentproject.main.index.fragment.fragmentA.FragmentA;
import com.example.gslih.componentproject.main.index.fragment.fragmentB.FragmentB;
import com.example.gslih.componentproject.main.index.fragment.fragmentC.FragmentC;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    FragmentA fragmentA;
    FragmentB fragmentB;
    FragmentC fragmentC;
    Fragment currentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentA = new FragmentA();
        currentFragment = fragmentA;

        getFragmentManager().beginTransaction().add(R.id.framelayout,fragmentA).commit();

        LinearLayout linearLayout = findViewById(R.id.bottom_a);
        LinearLayout linearLayout2 = findViewById(R.id.bottom_b);
        LinearLayout linearLayout3 = findViewById(R.id.bottom_c);



        linearLayout.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);

        TextView textView = findViewById(R.id.tv_bottom_a);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bottom_a){
            if (fragmentA == null){
                fragmentA = new FragmentA();
                getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.framelayout,fragmentA).commit();
            }else {
                getFragmentManager().beginTransaction().hide(currentFragment).show(fragmentA).commit();
            }
            currentFragment = fragmentA;
        }else if (view.getId() == R.id.bottom_b){
            if (fragmentB == null){
                fragmentB = new FragmentB();
                getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.framelayout,fragmentB).commit();
            }else {
                getFragmentManager().beginTransaction().hide(currentFragment).show(fragmentB).commit();
            }
            currentFragment = fragmentB;
        }else if (view.getId() == R.id.bottom_c){
            if (fragmentC == null){
                fragmentC = new FragmentC();
                getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.framelayout,fragmentC).commit();
            }else {
                getFragmentManager().beginTransaction().hide(currentFragment).show(fragmentC).commit();
            }
            currentFragment = fragmentC;
        }
    }
}
